﻿using System;
using System.Collections.Generic;
using System.Text;
using RockCompany.Common.Lib;
using RockCompany.Master.DTO;
using RockCompany.Common.Constant;
using System.Windows.Forms;
using System.Data;

namespace RockCompany.Master.Model
{
    public class SI05AFormModel : CommonModel
    {
        #region Properties
        private DataTable _newProdExportDetail;
        public DataTable newProdExportDetail
        {
            get { return _newProdExportDetail; }
            set { _newProdExportDetail = value; }
        }
        #endregion

        #region Constructor
        public SI05AFormModel()
        {
            newProdExportDetail = new DataTable();
            DataList = new List<object>();
            dbConnector = new DBConnector();
        }
        #endregion

        #region Utility Methods
        public DataRow getCheckedRow(int index)
        {
            DataRow curRow = newProdExportDetail.Rows[index];
            curRow.SetField<bool>(PHIEU_XH_CT_DTO.N_CHON, !curRow.Field<bool>(PHIEU_XH_CT_DTO.N_CHON));
            return curRow;
        }

        private DataTable buildprodInfo(DataTable selectProds)
        {
            string sProdList = "";
            foreach (DataRow row in selectProds.Rows)
            {
                sProdList += "'" + Util.parseString(row[CONG_LENH_SX.N_SO_LO]) + "',";
            }
            sProdList = sProdList.Substring(0, sProdList.Length - 1);

            string sSql = "SELECT A1." + TON_KHO_DTO.N_MA_HANG + " AS '" + CONG_LENH_SX.N_SO_LO + "',";
            sSql += "ISNULL(B1." + CONG_LENH_SX.N_MA_MAU + ",'') AS '" + CONG_LENH_SX.N_MA_MAU + "'";
            sSql += " FROM((SELECT *";
            sSql += " FROM " + TON_KHO_DTO.N_TABLE;
            sSql += " WHERE " + TON_KHO_DTO.N_MA_HANG + " IN(" + sProdList + ")) A1";
            sSql += " LEFT JOIN(SELECT *";
            sSql += " FROM " + CONG_LENH_SX.N_TABLE + ") B1";
            sSql += " ON A1." + TON_KHO_DTO.N_MA_HANG + "= B1." + CONG_LENH_SX.N_SO_LO + ") ";
            return selectData(sSql);
        }
        #endregion

        #region Main Methods
        public bool addNewProdExportDetail(string sUserName, DataTable selectedProd)
        {
            DataTable tempDB = buildprodInfo(selectedProd);
            if (tempDB != null && tempDB.Rows.Count > 0)
            {
                foreach (DataRow curRow in tempDB.Rows)
                {
                    DataRow newRow = newProdExportDetail.NewRow();
                    newRow.SetField<bool>(PHIEU_XH_CT_DTO.N_CHON, false);
                    newRow.SetField<string>(PHIEU_XH_CT_DTO.N_SO_LO, curRow.Field<string>(CONG_LENH_SX.N_SO_LO));
                    newRow.SetField<string>(PHIEU_XH_CT_DTO.N_MA_MAU, curRow.Field<string>(CONG_LENH_SX.N_MA_MAU));
                    newRow.SetField<decimal>(PHIEU_XH_CT_DTO.N_SL_XUAT, 0);
                    newRow.SetField<decimal>(PHIEU_XH_CT_DTO.N_TRONG_LUONG, 0);
                    newRow.SetField<string>(PHIEU_XH_CT_DTO.N_GHI_CHU, "");
                    newProdExportDetail.Rows.Add(newRow);
                }
            }
            return tempDB != null && tempDB.Rows.Count > 0;
        }

        public bool removeProdExportDetail(string sProdExportNo)
        {
            string sWhereClause = " WHERE ";
            sWhereClause += PHIEU_XH_CT_DTO.N_MS_PHIEU_XH + " ='" + sProdExportNo + "' AND ";
            sWhereClause += PHIEU_XH_CT_DTO.N_SO_LO + " IN(";
            Dictionary<string, object> pv = new Dictionary<string, object>();
            foreach (DataRow curRow in DataList)
            {
                if (curRow.RowState != DataRowState.Added)
                {
                    string sP = "@" + PHIEU_XH_CT_DTO.N_SO_LO + DataList.IndexOf(curRow).ToString();
                    sWhereClause += sP + ",";
                    pv.Add(sP, curRow.Field<string>(PHIEU_XH_CT_DTO.N_SO_LO));
                }
                newProdExportDetail.Rows.Remove(curRow);
            }
            DataList.Clear();
            if (pv.Count > 0)
            {
                sWhereClause = sWhereClause.Substring(0, sWhereClause.Length - 1) + ")";
                if (removeRow(PHIEU_XH_CT_DTO.N_TABLE, sWhereClause, pv))
                    pv.Clear();
            }
            return pv.Count == 0;
        }

        public bool insertNewProdExport(PHIEU_XH_DTO curProdExport)
        {
            Dictionary<string, object> pv = new Dictionary<string, object>();
            pv.Add(PHIEU_XH_DTO.N_MS_PHIEU_XH, curProdExport.MS_PHIEU_XH);
            pv.Add(PHIEU_XH_DTO.N_KHACH_HANG, curProdExport.KHACH_HANG);
            pv.Add(PHIEU_XH_DTO.N_DIA_CHI_KH, curProdExport.DIA_CHI_KH);
            pv.Add(PHIEU_XH_DTO.N_KEM_HOA_DON, curProdExport.KEM_HOA_DON);
            pv.Add(PHIEU_XH_DTO.N_PHE_DUYET, curProdExport.PHE_DUYET);

            pv.Add(PHIEU_XH_DTO.N_CREATION_TIME, DateTime.Now);
            pv.Add(PHIEU_XH_DTO.N_CREATION_PROGRAM_ID, "SI05A");
            pv.Add(PHIEU_XH_DTO.N_CREATION_USER, curProdExport.CREATION_USER);
            return insertData(PHIEU_XH_DTO.N_TABLE, pv);
        }

        public bool updateProdExport(PHIEU_XH_DTO curProdExport)
        {
            string sSetClause = " SET ";
            sSetClause += PHIEU_XH_DTO.N_PHE_DUYET + " = @" + PHIEU_XH_DTO.N_PHE_DUYET + ",";
            sSetClause += PHIEU_XH_DTO.N_KHACH_HANG + " = @" + PHIEU_XH_DTO.N_KHACH_HANG + ",";
            sSetClause += PHIEU_XH_DTO.N_DIA_CHI_KH + " = @" + PHIEU_XH_DTO.N_DIA_CHI_KH + ",";
            sSetClause += PHIEU_XH_DTO.N_KEM_HOA_DON + " = @" + PHIEU_XH_DTO.N_KEM_HOA_DON;

            string sClause = " WHERE ";
            sClause += PHIEU_XH_DTO.N_MS_PHIEU_XH + " = @" + PHIEU_XH_DTO.N_MS_PHIEU_XH;

            Dictionary<string, object> updateValues = new Dictionary<string, object>();
            updateValues.Add("@" + PHIEU_XH_DTO.N_MS_PHIEU_XH, curProdExport.MS_PHIEU_XH);
            updateValues.Add("@" + PHIEU_XH_DTO.N_PHE_DUYET, curProdExport.PHE_DUYET);
            updateValues.Add("@" + PHIEU_XH_DTO.N_KHACH_HANG, curProdExport.KHACH_HANG);
            updateValues.Add("@" + PHIEU_XH_DTO.N_DIA_CHI_KH, curProdExport.DIA_CHI_KH);
            updateValues.Add("@" + PHIEU_XH_DTO.N_KEM_HOA_DON, curProdExport.KEM_HOA_DON);

            updateValues.Add("@" + PHIEU_XH_DTO.N_LAST_UPDATE_TIME, DateTime.Now);
            updateValues.Add("@" + PHIEU_XH_DTO.N_LAST_UPDATE_PROGRAM_ID, "SI05A");
            updateValues.Add("@" + PHIEU_XH_DTO.N_LAST_UPDATE_USER, curProdExport.LAST_UPDATE_USER);
            return updateData(PHIEU_XH_DTO.N_TABLE, sSetClause, updateValues, sClause);
        }

        private bool insertNewProdExportDetail(string sCreator, string sProdExportNo, DataRow newRow)
        {
            Dictionary<string, object> pv = new Dictionary<string, object>();
            pv.Add(PHIEU_XH_CT_DTO.N_MS_PHIEU_XH, sProdExportNo);
            pv.Add(PHIEU_XH_CT_DTO.N_SO_LO, newRow.Field<string>(PHIEU_XH_CT_DTO.N_SO_LO));
            pv.Add(PHIEU_XH_CT_DTO.N_MA_MAU, newRow.Field<string>(PHIEU_XH_CT_DTO.N_MA_MAU));
            pv.Add(PHIEU_XH_CT_DTO.N_SL_XUAT, newRow.Field<decimal>(PHIEU_XH_CT_DTO.N_SL_XUAT));
            pv.Add(PHIEU_XH_CT_DTO.N_TRONG_LUONG, newRow.Field<decimal>(PHIEU_XH_CT_DTO.N_TRONG_LUONG));
            pv.Add(PHIEU_XH_CT_DTO.N_GHI_CHU, newRow.Field<string>(PHIEU_XH_CT_DTO.N_GHI_CHU));

            pv.Add(PHIEU_XH_CT_DTO.N_CREATION_PROGRAM_ID, "SI05A");
            pv.Add(PHIEU_XH_CT_DTO.N_CREATION_TIME, DateTime.Now);
            pv.Add(PHIEU_XH_CT_DTO.N_CREATION_USER, sCreator);
            return insertData(PHIEU_XH_CT_DTO.N_TABLE, pv);
        }

        private bool updateNewProdExportDetail(string sUpdater, string sProdExportNo, DataRow modRow)
        {
            string sSetClause = " SET ";
            sSetClause += PHIEU_XH_CT_DTO.N_SL_XUAT + " = @" + PHIEU_XH_CT_DTO.N_SL_XUAT + ",";
            sSetClause += PHIEU_XH_CT_DTO.N_TRONG_LUONG + " = @" + PHIEU_XH_CT_DTO.N_TRONG_LUONG + ",";

            sSetClause += PHIEU_XH_CT_DTO.N_LAST_UPDATE_TIME + " = @" + PHIEU_XH_CT_DTO.N_LAST_UPDATE_TIME + ",";
            sSetClause += PHIEU_XH_CT_DTO.N_LAST_UPDATE_PROGRAM_ID + " = @" + PHIEU_XH_CT_DTO.N_LAST_UPDATE_PROGRAM_ID + ",";
            sSetClause += PHIEU_XH_CT_DTO.N_LAST_UPDATE_USER + " = @" + PHIEU_XH_CT_DTO.N_LAST_UPDATE_USER;

            string sClause = " WHERE ";
            sClause += PHIEU_XH_CT_DTO.N_MS_PHIEU_XH + " = @" + PHIEU_XH_CT_DTO.N_MS_PHIEU_XH;
            sClause += " AND " + PHIEU_XH_CT_DTO.N_SO_LO + " = @" + PHIEU_XH_CT_DTO.N_SO_LO;

            Dictionary<string, object> updateValues = new Dictionary<string, object>();
            updateValues.Add("@" + PHIEU_XH_CT_DTO.N_MS_PHIEU_XH, sProdExportNo);
            updateValues.Add("@" + PHIEU_XH_CT_DTO.N_SO_LO, modRow.Field<string>(PHIEU_XH_CT_DTO.N_SO_LO));
            updateValues.Add("@" + PHIEU_XH_CT_DTO.N_SL_XUAT, modRow.Field<decimal>(PHIEU_XH_CT_DTO.N_SL_XUAT));
            updateValues.Add("@" + PHIEU_XH_CT_DTO.N_TRONG_LUONG, modRow.Field<decimal>(PHIEU_XH_CT_DTO.N_TRONG_LUONG));
            updateValues.Add("@" + PHIEU_XH_CT_DTO.N_LAST_UPDATE_TIME, DateTime.Now);
            updateValues.Add("@" + PHIEU_XH_CT_DTO.N_LAST_UPDATE_PROGRAM_ID, "SI05A");
            updateValues.Add("@" + PHIEU_XH_CT_DTO.N_LAST_UPDATE_USER, sUpdater);
            return updateData(PHIEU_XH_CT_DTO.N_TABLE, sSetClause, updateValues, sClause);
        }

        public bool saveData(string sProdExportNo, string sUserName)
        {
            DataTable tempDb = null;
            int insertRes = 0, updateRes = 0;

            tempDb = newProdExportDetail.GetChanges(DataRowState.Modified);
            int updatedRowCount = (tempDb != null ? tempDb.Rows.Count : 0);

            tempDb = newProdExportDetail.GetChanges(DataRowState.Added);
            int addedRowCount = (tempDb != null ? tempDb.Rows.Count : 0);

            tempDb = newProdExportDetail.GetChanges();
            if (tempDb != null && tempDb.Rows.Count > 0)
            {
                foreach (DataRow curRow in tempDb.Rows)
                {
                    switch (curRow.RowState)
                    {
                        case DataRowState.Added:
                            if (insertNewProdExportDetail(sUserName, sProdExportNo, curRow))
                                insertRes++;
                            break;
                        case DataRowState.Modified:
                            if (updateNewProdExportDetail(sUserName, sProdExportNo, curRow))
                                updateRes++;
                            break;
                    }
                }
                newProdExportDetail.AcceptChanges();
            }
            return insertRes == addedRowCount && updateRes == updatedRowCount;
        }
        #endregion
    }
}
